/*
 * Project: Daycare Web Application
 * Package: com.madetoordersoftware.daycare.control
 * File:    ParentCtrl
 * Created: Jun 3, 2010
 */
/**
 * Copyright (c) 2010 Made 2 Order Software, LLC
 *
 * All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 * transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 * You may not distribute the URL or redistribute the files to third parties. You may transfer the files onto
 * a single storage device so long as you can prove ownership of the software. You may not reverse engineer,
 * decompile, or disassemble the software or media files. You may not modify or translate the software or
 * media, or distribute copies of the software or media without the written consent of Made 2 Order Software,
 * LLC.
 */
package com.made2ordersoftware.daycare.control;

import com.made2ordersoftware.control.AbstractCtrl;
import com.made2ordersoftware.daycare.business.ParentMgr;
import com.made2ordersoftware.daycare.control.util.JsfUtil;
import com.made2ordersoftware.daycare.domain.enums.State;
import com.made2ordersoftware.exceptions.DAOException;
import com.made2ordersoftware.exceptions.InvalidFormatException;
import com.made2ordersoftware.util.ExceptionHandler;
import java.util.ResourceBundle;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Control class for all parent actions.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
@Named("parent")
@RequestScoped
public class ParentCtrl extends AbstractCtrl {

    private static final Logger LOG = LoggerFactory.getLogger(ParentCtrl.class);
    private static final long serialVersionUID = 1L;
    @Inject
    private ParentMgr manager;
    private String firstName;
    private String lastName;
    private String street;
    private String city;
    private State state;
    private String zip;
    private String workPhone;
    private String primaryPhone;
    private String secondaryPhone;
    private String email;
    private State states;

    /**
     * Creates a new instance of <code>ParentCtrl</code>.
     */
    public ParentCtrl() {
    }

    /**
     * Adds a new parent to the system.
     *
     * @return jsf page to navigate to when complete.
     */
    public String create() {
        LOG.info("Creating parent in control class.");
        String nextPage = null;
        Exception exp = null;
        String message = null;
        try {
            manager.addParent(getFirstName(), getLastName(), getStreet(), getCity(), getState(), getZip(),
                    getWorkPhone(), getPrimaryPhone(), getSecondaryPhone(), getEmail());
            message = ResourceBundle.getBundle("/Bundle").getString("parent_created");
            nextPage = "/home";
            LOG.info("Parent added successfully.");
        } catch (InvalidFormatException e) {
            exp = e;
            message = ResourceBundle.getBundle("/Bundle").getString("field_format_message");
        } catch (DAOException e) {
            exp = e;
            message = ResourceBundle.getBundle("/Bundle").getString("persistence_error_occured");
            nextPage = "serverError";
        } finally {
            if (exp != null) {
                ExceptionHandler.handleException(exp);
                JsfUtil.addErrorMessage(message);
            } else {
                JsfUtil.addSuccessMessage(message);
            }
        }
        return nextPage;
    }

    /**
     * @return the firstName
     */
    public String getFirstName() {
        return firstName;
    }

    /**
     * @param firstName the firstName to set
     */
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    /**
     * @return the lastName
     */
    public String getLastName() {
        return lastName;
    }

    /**
     * @param lastName the lastName to set
     */
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    /**
     * @return the street
     */
    public String getStreet() {
        return street;
    }

    /**
     * @param street the street to set
     */
    public void setStreet(String street) {
        this.street = street;
    }

    /**
     * @return the city
     */
    public String getCity() {
        return city;
    }

    /**
     * @param city the city to set
     */
    public void setCity(String city) {
        this.city = city;
    }

    /**
     * @return the state
     */
    public State getState() {
        return state;
    }

    /**
     * @param state the state to set
     */
    public void setState(State state) {
        this.state = state;
    }

    /**
     * @return the zip
     */
    public String getZip() {
        return zip;
    }

    /**
     * @param zip the zip to set
     */
    public void setZip(String zip) {
        this.zip = zip;
    }

    /**
     * @return the workPhone
     */
    public String getWorkPhone() {
        return workPhone;
    }

    /**
     * @param workPhone the workPhone to set
     */
    public void setWorkPhone(String workPhone) {
        this.workPhone = workPhone;
    }

    /**
     * @return the primaryPhone
     */
    public String getPrimaryPhone() {
        return primaryPhone;
    }

    /**
     * @param primaryPhone the primaryPhone to set
     */
    public void setPrimaryPhone(String primaryPhone) {
        this.primaryPhone = primaryPhone;
    }

    /**
     * @return the secondaryPhone
     */
    public String getSecondaryPhone() {
        return secondaryPhone;
    }

    /**
     * @param secondaryPhone the secondaryPhone to set
     */
    public void setSecondaryPhone(String secondaryPhone) {
        this.secondaryPhone = secondaryPhone;
    }

    /**
     * @return the email
     */
    public String getEmail() {
        return email;
    }

    /**
     * @param email the email to set
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * @return the states
     */
    public State[] getStates() {
        return State.values();
    }
}
